package com.controller.permission;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.po.Result;
import com.po.UserInfo;
import com.po.UserRegisterInfo;
import com.service.infocenter.RulePointService;
import com.service.infocenter.UserRegisterService;
import com.service.infocenter.UserService;
import com.util.MD5Util;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * 拦截器触发不了
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/jsp/permission")
public class Logincontroller {

    @Resource(name = "userServiceImp")
    private UserService userService;
    @Resource(name = "userRegisterServiceImp")
    private UserRegisterService userRegisterService;
    @Resource(name = "rulePointServiceImp")
    private RulePointService rulepointService;

    /**
     * 登录
     * 
     * @param user_id
     * @param user_password
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getUserLogin")
    public Object getUserLogin(String user_id, String user_password, HttpServletRequest request) {
	int rescode = 0;
	if (userService.getUserLogin(user_id, user_password, request)) {
	    int i = userService.addLoginhistory(user_id);
	    int j = userService.updateUserLastTime(user_id);
	    if (i == 1) {
		rescode = 1;
	    }
	}
	return rescode;
    }

    /**
     * 退出登录
     * 
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/loginout")
    public Object logout(HttpServletRequest request) {
	int rescode = 1;
	request.getSession().removeAttribute("user_id");
	request.getSession().invalidate();
	return rescode;
    }

    /**
     * 判断用户时候已经存在，修改密码时使用
     * 
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/haveuser")
    public Result haveuser(String telephonename) {
	UserRegisterInfo userRegister = new UserRegisterInfo();
	userRegister.setUser_id(telephonename);
	boolean bool = userRegisterService.addUserRegisterIsOk(userRegister);
	Result result = new Result();
	result.setValid(bool);
	return result;
    }

    /**
     * 判断用户时候已经存在，修改密码时使用
     * 
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/haveuserno")
    public Result haveuserno(String telephonename) {
	UserRegisterInfo userRegister = new UserRegisterInfo();
	userRegister.setUser_id(telephonename);
	boolean bool = userRegisterService.addUserRegisterIsOk(userRegister);
	Result result = new Result();
	result.setValid(!bool);
	return result;
    }

    /**
     * 判断用户时候已经存在，修改密码时使用
     * 
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/changepwd")
    public int changepwd(UserRegisterInfo userRegister, HttpServletRequest request) {
	int i = userRegisterService.updatepwd(userRegister);
	request.getSession().invalidate();
	return i;
    }

    /**
     * 用户注册
     * 
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/register")
    public int register(UserInfo user) {
	int i = userService.addUser(user);
	if (i > 0) {
	    int point = rulepointService.getRegisterPoint("9");
	    rulepointService.updateUserpoint(user.getUser_id(), point);
	}
	return i;
    }

    /**
     * 手机验证码请求验证
     * 
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/CheckPhone")
    public String Register(HttpServletRequest request, String phone) throws Exception {
	// 在次数随机生成验证码
	URL urL = new URL(
		"http://211.149.203.162:8868/sms.aspx?action=send&userid=195&account=sxrcfz&password=cxrc85309181&mobile="
			+ phone + "&content=【创新人才】您本次的验证码是456896，欢迎您注册!");
	HttpURLConnection con = (HttpURLConnection) urL.openConnection();
	con.setRequestMethod("GET");
	con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
	con.setRequestProperty("Connection", "keep-alive");
	con.setRequestProperty("Host", "211.149.203.162:8868");
	con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/57.0");
	con.setDoOutput(true);
	con.setDoInput(true);
	con.connect();
	InputStream in = con.getInputStream();
	BufferedReader reader = new BufferedReader(new InputStreamReader(in));
	String line = null;
	StringBuffer html = new StringBuffer();
	while ((line = reader.readLine()) != null) {
	    html.append(line);
	}
	reader.close();

	Document doc = null;

	doc = DocumentHelper.parseText(html.toString()); // 将字符串转为XML
	Element rootElt = doc.getRootElement(); // 获取根节点
	String bool = rootElt.elementTextTrim("returnstatus"); // 获取根节点下的子节点head

	return bool;
    }

    /**
     * 判断用户时候已经存在，修改密码时使用
     * 
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pwdistrue")
    public Result pwdIstrue(String oldpassword, HttpServletRequest request) {
	String userid = request.getSession().getAttribute("user_id").toString();
	UserRegisterInfo user = new UserRegisterInfo();
	user.setUser_id(userid);
	boolean bool = false;
	user = userRegisterService.getUserRegisterListByID(user);
	if (MD5Util.getMD5(oldpassword).equals(user.getUser_password()))
	    bool = true;
	Result result = new Result();
	result.setValid(bool);
	return result;
    }
}
